/**
 * Copyright (C) 2008 Wideplay Interactive.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.neodatis.warp;

import net.jcip.annotations.Immutable;

import org.neodatis.odb.ODBFactory;
import org.neodatis.odb.ODBServer;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Provider;
import com.wideplay.warp.util.LazyReference;

/**
 * @author Jeffrey Chung (jeffreymchung@gmail.com)
 * @author Robbie Vanbrabant
 */
@Immutable
class ODBServerProvider implements Provider<ODBServer> {
    /**
     * Lazily loaded ObjectServer.
     */
    private LazyReference<ODBServer> objectServer =
            LazyReference.of(new Provider<ODBServer>() {
                public ODBServer get() {
                    NeodatisSettings actualSettings = getSettings();

                    //validate configuration object
                    if ( (!HostKind.FILE.equals(actualSettings.getHostKind())) && null == actualSettings.getConfiguration())
                        throw new IllegalStateException("Must specify a Configuration when using " + actualSettings.getHost() + " server mode." +
                                " For starters, try: bind(Configuration.class).toInstance(new Configuration());");

                    //use local (i.e. open our own) object server?
                    if (HostKind.LOCAL.equals(actualSettings.getHostKind())) {
                    	// TODO how about setting a Configuration?
//                    	ODBServer odbServer = ODBFactory.openServer(actualSettings.getConfiguration(), actualSettings.getDatabaseFileName(), actualSettings.getPort());

                    	ODBServer odbServer = ODBFactory.openServer(actualSettings.getPort());
                    	// TODO configure db names
                    	odbServer.addBase("base1", settings.getDatabaseFileName());
                    	odbServer.startServer(true);
                    	
                        // TODO auth if credentials are available
//                        if (!empty(actualSettings.getUser()))
//                            odbServer.grantAccess(actualSettings.getUser(), actualSettings.getPassword());

                        return odbServer;
                        
                    	}
                    	// TODO do nothing here? / otherwise it's a simple local-file database
//                        else if (HostKind.FILE.equals(actualSettings.getHostKind())) {
//                    	return ODBFactory.openServer(actualSettings.getDatabaseFileName(), actualSettings.getPort());
//                    }
                    // remote, fake objectServer.
//                    return new NullODBServer();
                    throw new RuntimeException("NullODBServer not yet implemented");
                }
            });

    @Inject
    private final Injector injector = null;

    private final NeodatisSettings settings;

    public ODBServerProvider(NeodatisSettings settings) {
        this.settings = settings;
    }

    public ODBServer get() {
		return this.objectServer.get();
	}

    protected NeodatisSettings getSettings() {
        return this.settings != null ?
                this.settings :
                injector.getInstance(NeodatisPersistenceStrategy.NeodatisPersistenceStrategyBuilder.class).buildNeodatisSettings();
    }
}
